#ifndef ROUTEMAP_H
#define ROUTEMAP_H
#include<algorithm>
#include "gamehandler.h"
#include <sigc++/sigc++.h>

using namespace std;
using namespace Glib;
using namespace Gio;

namespace vectorMap{

class Node;
class RouteMap;
class Route;

struct Path{
	string name;
    Node* from;
    Node* to;
    Path(Node* f=0, Node* t=0){
        from =f;
        to = t;
    }
	virtual double lengthSquare();
	virtual long length(){
		if(!from || !to)
			return INT_MAX;
		return sqrt(lengthSquare());
	}
};
/** Place
 * An euclidean place on the map. you can extend it and make it non-euclidean, but then you can't use anymore.
 * 
 * */
struct Place{ 
    int x;
    int y;
    /**
    the square of the euclidean distance to \c otherplace
    */
    virtual double lengthSquare(Place* otherplace);
	virtual int length(Place* otherplace){
		return sqrt(lengthSquare(otherplace));
	}
};
/** NodeId
 * this is the way of identifying nodes. It now is just sort of an extention of int, but you can extend it to fit your game-environment. as long as you implement the operators rigth.
 * for instance UnrealNode has a string like "mapname.nodetype42"
 */

struct NodeId{
int id;
NodeId(int i=-1):id(i){}

virtual int operator=(int i){
    return id=i;
}
virtual bool operator==(int i){
    return i==id;
}

};


/**
 * MapObject
 * this is basically something that exists in the map. They are all treated the same, except for Nodes.
 * If you extend this class, it might be also be usefull to extend the \c drawables::vectorMap::mapDrawable class as well.
 * */
 
struct MapObject{
    Place* place; /** the euclidean location of the node*/
	RouteMap* map;
	
	virtual string getName()
	{
		return ustring::compose( "object@(%1, %2)" , place->x ,place->y);
	}

	MapObject(RouteMap* map, Place* p=0):map(map){
		place=p;
	}
};
struct Node:MapObject{
    NodeId* id; /** \c NodeId this is a pointer to a nodeId. You can extend nodeId to tag your nodes in an other way than with an \c int
                this is to make shure that the vector with nodes doesn't have to be filled with pointers wich makes iterating easier*/
	friend class RouteMap;
	ustring name;
	virtual string getName(){
		return name;
	}
	protected:
    Node(RouteMap* map):MapObject(map){
         id   = 0;
        place = 0;
    }
	public:
    vector<Path*> outgoing;/** vector with outgoing pahts. ie alle the in this vector have this as from*/
    vector<Path*> incoming;/** vector with incoming pahts. ie alle the in this vector have this as to */
};


/**
 * \brief RouteMap
 * 
 * a map representing an euclidean (portals should be possible, but hard to draw) game-map.
 * It contains a list of nodes connected by paths. For routing it uses floyd-warshall.
 * 
 **/
class RouteMap{
	friend class Node;
	
    public:
        int left, right, top, bottom; // the extreme coordinates of the map; ie left is the most left vertex;
        int maxId, maxCategory;
        std::vector<Node> nodes;
        bool filled;
        bool finalized;

		int **FWmap;
		int **FWnextMap;
		
        /** Default constructor */
        RouteMap();
		public: Node* createNode(string name=""){
			Node* nn = new Node(this);
			nn->name=name;
			return nn;
		}
		
		Route* route(Node* from, Node* to){
			return route(getNodeIndex(from), getNodeIndex(to));
		}
		
		Route* route(int fromIndex, int toIndex);
		
        void finalize();
		int getNodeIndex(Node* node);
		
		virtual void invalidate();
		
		virtual sigc::signal<bool> signal_invalidate();
        /** Default destructor */
            virtual ~RouteMap();
protected:
		sigc::signal<bool> m_signal_invalidate;
    private:
};
}
#endif // ROUTEMAP_H
